package com.example.baidumapdemo.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.example.baidumapdemo.R;
import com.example.baidumapdemo.fragment.LeftFragment;
import com.example.baidumapdemo.fragment.RightFragment;

public class MapActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnRightFragment;
    private Button btnLeftFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        btnLeftFragment = findViewById(R.id.btn_left_fragment);
        btnRightFragment = findViewById(R.id.btn_right_fragment);
        btnLeftFragment.setOnClickListener(this);
        btnRightFragment.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_left_fragment:
                replaceFragment(new LeftFragment());
                break;
            case R.id.btn_right_fragment:
                replaceFragment(new RightFragment());
                break;
            default:
                break;
        }
    }

    public void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.frame_layout, fragment);
        transaction.commit();
    }
}